package com.yaodian100.mall.announcement;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.announcement.entity.Announcement;
import com.yaodian100.ec.announcement.service.AnnouncementService;

/**
 * 公告檢視
 * 
 * @author apple.chang
 * 
 */
public class AnnouncementAction extends ActionSupport implements Preparable {

	private static final String RESULT_VIEW_MEMBER_MESSAGE = "viewMemberMessage";
	private static final String REULST_LIST_ALL_MEMBER_MESSAGE = "listAllMemberMessage";

	/**
	 * 
	 */
	private static final long serialVersionUID = 6869878503316754836L;

	private AnnouncementService announcementService;
	private String targetURL;
	private String anncontent;
	private String annNameParam;
	private String startDateParam;
	private String endDateParam;
	private String annTypeParam;
	private String annId;
	private SimplePager pager;
	private String orderByDate = "desc";
	private long announcementSize;
	private List<Announcement> announcementList;

	/**
	 * 檢視公告
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String viewv2() throws CoreException {
		Announcement announcement = announcementService.getVisibleAnnouncementById(annId);

		if (announcement == null) {
			return ERROR;
		}

		if ("N".equals(announcement.getIsDelete())) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			startDateParam = df.format(announcement.getStartDate());
			annNameParam = announcement.getSubject();
			if (StringUtils.isNotBlank(announcement.getBody())) {
				anncontent = announcement.getBody();
			}
		}
		return "view";
	}

	public String viewMemberMessage() throws CoreException {
		Announcement announcement = announcementService.getVisibleAnnouncementById(annId);

		if (announcement == null) {
			return ERROR;
		}

		if ("N".equals(announcement.getIsDelete())) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			startDateParam = df.format(announcement.getStartDate());
			annNameParam = announcement.getSubject();
			if (StringUtils.isNotBlank(announcement.getBody())) {
				anncontent = announcement.getBody();
			}
		}

		return RESULT_VIEW_MEMBER_MESSAGE;
	}

	public String listAll() {
		CommonCriteria crit = new CommonCriteria();
		crit.addLe("startDate", new Date());
		crit.addGe("startDate", DateUtils.addYears(new Date(), -1));
		crit.addEq("isDelete", "N");
		crit.addEq("type", "MYYAODIAN");

		announcementSize = announcementService.getListSize(crit).longValue();
		pager.setPageRecord(20);// defaut 20
		pager.setTotalSize(announcementSize);
		announcementList = announcementService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, new String[] { "startDate " + orderByDate, AnnouncementService.SHOW_SEQ_SORT_CLAUSE,
				"modifyDate desc" });
		return "listAll";
	}

	public String listAllMemberMessage() {
		Date now = new Date();
		CommonCriteria crit = new CommonCriteria();
		crit.addLe("startDate", now);
		crit.addGe("startDate", DateUtils.addYears(new Date(), -1));
		crit.addGe("endDate", now);
		crit.addEq("isDelete", "N");
		crit.addEq("type", AnnouncementService.ANNOUNCEMENT_TYPE_MEMBER_MESSAGE);

		this.announcementSize = announcementService.getListSize(crit).longValue();
		this.pager.setPageRecord(20);// defaut 20
		this.pager.setTotalSize(announcementSize);
		this.announcementList = this.announcementService.getList(this.pager.getCurrentPage() * this.pager.getPageRecord(),
				this.pager.getPageRecord(), crit, new String[] { "startDate " + this.orderByDate,
						AnnouncementService.SHOW_SEQ_SORT_CLAUSE, "modifyDate desc" });

		return REULST_LIST_ALL_MEMBER_MESSAGE;
	}

	public void prepare() throws Exception {
		if (null == pager) {
			pager = new SimplePager(1);
		}
	}

	public AnnouncementService getAnnouncementService() {
		return announcementService;
	}

	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public String getAnnNameParam() {
		return annNameParam;
	}

	public void setAnnNameParam(String annNameParam) {
		this.annNameParam = annNameParam;
	}

	public String getAnnTypeParam() {
		return annTypeParam;
	}

	public void setAnnTypeParam(String annTypeParam) {
		this.annTypeParam = annTypeParam;
	}

	public String getTargetURL() {
		return targetURL;
	}

	public void setTargetURL(String targetURL) {
		this.targetURL = targetURL;
	}

	public String getAnncontent() {
		return anncontent;
	}

	public void setAnncontent(String anncontent) {
		this.anncontent = anncontent;
	}

	public String getAnnId() {
		return annId;
	}

	public void setAnnId(String annId) {
		this.annId = annId;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public String getOrderByDate() {
		return orderByDate;
	}

	public void setOrderByDate(String orderByDate) {
		this.orderByDate = orderByDate;
	}

	public long getAnnouncementSize() {
		return announcementSize;
	}

	public void setAnnouncementSize(long announcementSize) {
		this.announcementSize = announcementSize;
	}

	public List<Announcement> getAnnouncementList() {
		return announcementList;
	}

	public void setAnnouncementList(List<Announcement> announcementList) {
		this.announcementList = announcementList;
	}

}